package com.sayyi.demo.swagger.controller;

import com.sayyi.demo.swagger.common.ResultBean;
import com.sayyi.demo.swagger.domain.Employee;
import com.sayyi.demo.swagger.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 员工信息管理
 * @author xuchuang
 * date 2021/12/23 3:36 PM
 */
@RestController
@RequestMapping("/employee")
@Slf4j
@Tag(name = "员工信息管理", description = "员工信息管理控制器")
public class EmployeeController {

    private EmployeeService employeeService;
    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 通过id获取员工信息
     * @param id    员工id
     * @return  员工信息。可能为空
     */
    @GetMapping("/{id}")
    @Operation(
            // 错误定义的信息，会被 功能性注解的实际内容 覆盖掉
            method = "POST",
            description = "通过id获取员工信息，如果员工不存在则返回空值"
    )
    public ResultBean<Employee> getById(@PathVariable("id") long id) {
        return ResultBean.success(employeeService.getById(id));
    }

    /**
     * 保持员工信息
     * @param employee  员工信息
     * @return  员工id
     */
    @PutMapping("/")
    @Operation(
            description = "保存用户信息",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "员工信息，不传id"),
            responses = {@ApiResponse(description = "员工id", responseCode = "200")}
    )
    public ResultBean<Long> save(@RequestBody @Valid Employee employee) {
        log.debug("save employee [{}]", employee);
        return ResultBean.success(employeeService.save(employee));
    }
}
